
import V1 from "./index";
import userModel from "../../models/user";
import test from "../../util/test";


class User extends V1 {
  constructor() {
    super();
    this.send_email = this.send_email.bind(this);
    this.login = this.login.bind(this);
    this.user_create = this.user_create.bind(this);
    this.put_message = this.put_message.bind(this);
    this.put_pwd = this.put_pwd.bind(this);
    this.retrieve_pwd = this.retrieve_pwd.bind(this);
  }


  /**
   * 发送验证码 type 1 注册 2 忘记密码 email --
   * @returns {Promise<void>}
   */
    async send_email(req, res) {

     const body = await this.email(req.body);
     res.send(body)
  }

  /**
   * 用户的登录
   */
  async login(req, res) {
    const body = {
      status: 200,
      msg: '请求成功'
    };

    const {user_tel, user_pwd, user_email} = req.body;
    try {
      if(!test.mobile(user_tel) && test.isEmpty(user_email)) throw ('手机号有误');
      if(!test.email(user_email) && test.isEmpty(user_tel)) throw ('邮箱账号有误');
      const user_item = await userModel.findOne({$or: [{user_tel: user_tel}, {user_email}]})
      if(!user_item) throw ('没有此用户');
      if(this.decrypt_Pkcs7(user_item.user_pwd) !== user_pwd) {
        // body.pwd = this.decrypt_Pkcs7(user_item.user_pwd);
        // body.data = user_item
        throw ('密码错误')
      }
      body.data = user_item._doc
      body.data.token = this.sign_jwt({userId:user_item._id, userCreate: user_item.user_create, userTel: user_item.user_tel, userEmail: user_item.user_email})
      delete body.data.user_pwd

    } catch (e) {
      body.status = 201
      body.msg = e.toString();
    }

    res.send(Object.assign({}, body, req.user_info))
  }

  /**
   * 创建用户
   */
  async user_create(req, res) {
    const body = {
      status: 200,
      msg: '注册成功'
    };
    const {user_email, user_pwd, code, user_tel} = req.body;

    try {
      if(!user_pwd || !user_tel) throw ('参数无效');
      if(!test.mobile(user_tel)) throw ('手机号有误');
      if(user_pwd.length < 6 || user_pwd.length > 18) throw('请设置密码6-18位');
      const is_create = await userModel.findOne({$or: [{user_tel: user_tel *1}, {user_email}]});
      // await userModel.deleteMany({$or: [{user_tel: user_tel *1}, {user_email}]})
      if(is_create) throw ('此手机号或邮箱已被注册');
      // 验证码是否有效
      const is_send_email = await this.is_email_code({type: '1', email: user_email, code: code});
      if(is_send_email.status !== 200) {
        body.status = is_send_email.status;
        throw (is_send_email.msg)
      }
      const user_item = await userModel.create({user_email, user_pwd: this.encrypt_Pkcs7(user_pwd), user_tel})
      body.data = user_item._doc;
      body.data.token = this.sign_jwt({userId:user_item._id, userCreate: user_item.user_create, userTel: user_item.user_tel, userEmail: user_item.user_email})
      delete body.data.user_pwd;
    } catch (e) {
      if(body.status === 200) {
        body.status = 201;
      }
      body.msg = e.toString();
    }

    res.send(Object.assign({}, body))
  }


  /**
   * 找回密码
   */
  async retrieve_pwd(req, res) {
    const body = {
      status: 200,
      msg: '修改成功'
    };
    const {user_email, user_pwd, code} = req.body;

    try {
      if(!user_pwd) throw ('参数无效');
      if(user_pwd.length < 6 || user_pwd.length > 18) throw('请设置密码6-18位');
      const is_create = await userModel.findOne( {user_email});
      if(!is_create) throw ('此邮箱未注册用户账号');
      // 验证码是否有效
      const is_send_email = await this.is_email_code({type: '2', email: user_email, code});
      if(is_send_email.status !== 200) {
        body.status = is_send_email.status;
        throw (is_send_email.msg)
      }
      await userModel.updateOne({user_email}, { user_pwd: this.encrypt_Pkcs7(user_pwd), user_use_state: '2'})
    } catch (e) {
      if(body.status === 200) {
        body.status = 201;
      }
      body.msg = e.toString();
    }

    res.send(Object.assign({}, body, ))
  }

  /**
   * 修改密码
   */
  async put_pwd(req, res) {
    const body = {
      status: 200,
      msg: '新密码修改成功'
    };

    const {new_user_pwd, user_pwd} = req.body;

    body.info = req.user_info;
    try {
      if(!user_pwd || !new_user_pwd) throw ('参数无效');
      const is_create = await userModel.findOne( {_id: req.user_info.userId});
      if(!is_create) throw ('此用户不存在或删除');
      if(user_pwd !== this.decrypt_Pkcs7(is_create.user_pwd))  throw ('原密码错误');
      if(new_user_pwd.length < 6 || new_user_pwd.length > 18) throw('请设置新密码6-18位');
      await userModel.updateOne({user_email: is_create.user_email, _id: req.user_info.userId}, { user_pwd: this.encrypt_Pkcs7(new_user_pwd), user_age: '1'})
    } catch (e) {
      if(body.status === 200) {
        body.status = 201;
      }
      body.msg = e.toString();
    }


    res.send(Object.assign({}, body))
  }


  /**
   * 修改个人信息
   */
  put_message(req, res) {
    const body = {
      status: 200,
      msg: '请求成功'
    };
    res.send(Object.assign({}, body, {a: req.body, b: req.query, h: req.headers}))
  }



}

export default User;
